import { getConfig, statistics } from '@/api/app'
import { defineStore } from 'pinia'

function getNum(count:number = 0){
  return count > 99 ? '99+' : count
}
interface AppSate { 
    config: Record<string, any>
    isMobile: boolean
    isCollapsed: boolean
    isRouteShow: boolean
    openSide:boolean
    
    unAuditCertitified:number | string,
    unExhibitor:number | string,
    unShop:number | string
    unShopAll:number | string,
    unHuik:number | string,
    unDemand:number | string,
    unFenp:number | string,
    unWithdrawal:number | string,
    unInvoiceApply:number | string,
    unRefundInvoice:number | string,
    unPark:number | string,
    badgeMaking:number | string,
    unConcurrentEevents:number | string,
    
    my_unAuditCertitified:number | string,
    my_unExhibitor:number | string,
    my_unShop:number | string
    my_unShopAll:number | string,
    my_unHuik:number | string,
    my_unDemand:number | string,
    my_unFenp:number | string,
    my_unWithdrawal:number | string,
    my_unInvoiceApply:number | string,
    my_unRefundInvoice:number | string,
    my_unPark:number | string,
    my_badgeMaking:number | string,
    my_unConcurrentEevents:number | string,
}

const useAppStore = defineStore({
    id: 'app',
    state: (): AppSate => {
        return {
            config: {},
            isMobile: true,
            isCollapsed: false,
            isRouteShow: true,
            openSide:false, // 1-手动操作， 0-无二级自动
            
            unAuditCertitified:0, //未审核的用户认证数量
            unExhibitor:0, // 未审核的展商数量
            unShop:0, // 未审核的钓具店数量
            unShopAll:0, // 未审核的商家数量
            unHuik:0, // 未审核的汇款记录数量
            unDemand:0, //  未审核的需求数量
            unFenp:0, //  展位预订待分配数量
            unWithdrawal:0, // 提现记录待分配数量
            unInvoiceApply:0, // 申请发票 - 待分配数量
            unRefundInvoice:0, // 退票申请
            unPark:0, // 车牌证
            badgeMaking:0, // 胸卡未审核数量
            unConcurrentEevents:0,//同期服务未审核数量
            
            // 个人的
            my_unAuditCertitified:0, //未审核的用户认证数量
            my_unExhibitor:0, // 未审核的展商数量
            my_unShop:0, // 未审核的钓具店数量
            my_unShopAll:0, // 未审核的商家数量
            my_unHuik:0, // 未审核的汇款记录数量
            my_unDemand:0, //  未审核的需求数量
            my_unFenp:0, //  展位预订待分配数量
            my_unWithdrawal:0, // 提现记录待分配数量
            my_unInvoiceApply:0, // 申请发票 - 待分配数量
            my_unRefundInvoice:0, // 退票申请
            my_unPark:0, // 车牌证
            my_badgeMaking:0, // 胸卡未审核数量
            my_unConcurrentEevents:0,//同期服务未审核数量
        }
    },
    actions: {
        getImageUrl(url: string) {
            return url.indexOf('http') ? `${this.config.oss_domain}${url}` : url
        },
        setOpenSide(e:boolean) {
            this.openSide = e;
        },
        getConfig() {
            return new Promise((resolve, reject) => {
                getConfig()
                    .then((data) => {
                        this.config = data
                        resolve(data)
                    }) 
                    .catch((err) => {
                        reject(err)
                    })
            })
        },
        setMobile(value: boolean) {
            this.isMobile = value
        },
        toggleCollapsed(toggle?: boolean) {
            this.isCollapsed = toggle ?? !this.isCollapsed
        },
        refreshView() {
            this.isRouteShow = false
            nextTick(() => {
                this.isRouteShow = true
            })
        },
        
        setUnAuditCertitified(user_id:number = ''){
            statistics({user_id: user_id}).then(res=>{
              if(user_id){
                this.my_unWithdrawal = getNum(res.withdrawal_record);  // 提现记录待分配数量
                this.my_unHuik = getNum(res.remitLogCount); //  汇款记录数量
                this.my_unInvoiceApply = getNum(res.invoiceApplyCount); // 发票申请数量
                this.my_unRefundInvoice = getNum(res.refundInvoiceCount); // 退票申请数量
                this.my_unAuditCertitified = getNum(res.userApplyCount); //认证申请数量
                this.my_unShop = getNum(res.fishShopCount); // 钓具店申请数量
                this.my_unPark = getNum(res.parkingSpace); // 车牌登记数量
                this.my_badgeMaking = getNum(res.badgeMaking); // 胸卡待审核数量
                this.my_unExhibitor = getNum(res.exhibitorCount); // 展商申请数量
                this.my_unFenp = getNum(res.preOrderCount); //  待分配数量
                this.my_unDemand = getNum(res.demandCount); //  需求申请数量
                this.my_unConcurrentEevents = getNum(res.concurrent_events); //  同期服务申请数量
              }else{
                this.unWithdrawal = getNum(res.withdrawal_record);  // 提现记录待分配数量
                this.unHuik = getNum(res.remitLogCount); //  汇款记录数量
                this.unInvoiceApply = getNum(res.invoiceApplyCount); // 发票申请数量
                this.unRefundInvoice = getNum(res.refundInvoiceCount); // 退票申请数量
                this.unAuditCertitified = getNum(res.userApplyCount); //认证申请数量
                this.unShop = getNum(res.fishShopCount); // 钓具店申请数量
                this.unPark = getNum(res.parkingSpace); // 车牌登记数量
                this.badgeMaking = getNum(res.badgeMaking); // 胸卡待审核数量
                this.unExhibitor = getNum(res.exhibitorCount); // 展商申请数量
                this.unFenp = getNum(res.preOrderCount); //  待分配数量
                this.unDemand = getNum(res.demandCount); //  需求申请数量
                this.unConcurrentEevents = getNum(res.concurrent_events); //  同期服务申请数量
                this.unShopAll = res.fishShopCount + res.parkingSpace + res.fishShopCount; // 商家管理的所有位审核数量
              }
            })
            
        }
    }
})

export default useAppStore
